﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAppViaWebService.Forms;
using ClientAppViaWebService.Utility;
using ClientAppViaWebService.BaseLogicServiceRef;

namespace ClientAppViaWebService.Navigators
{
    public partial class BaseNavigationControl : UserControl
    {
        protected BaseLogicServiceRef.BaseLogicService service;

        public TObject ActiveNode { get; set; }
        protected List<UniViewActionMenuItem> optMenu = new List<UniViewActionMenuItem>();
        public string RootId { get; set; }

        public BaseNavigationControl()
        {
            InitializeComponent();
        }      

        private void uniTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Tag is TObject)
            {
                TreeNode node = e.Node;
                TreeViewLogic.LoadChildrenByParentTobj(service, ref node, e.Node.Tag as TObject, ref itemContextMenuStrip);
            }
            return;
        }

        private void uniTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            uniTreeView.SelectedNode = e.Node;
            if (e.Node.Tag.ToString().Equals("-1"))
            {
                ActiveNode = null;
                return;
            }

            if (uniTreeView.SelectedNode.Tag is TObject && ((TObject)uniTreeView.SelectedNode.Tag).CLASS_ID != 0)
            {
               ActiveNode = uniTreeView.SelectedNode.Tag as TObject;
            }

            if (e.Button == MouseButtons.Right)
            {
                uniTreeView.SelectedNode = e.Node;
                itemContextMenuStrip.Items[1].Visible = itemContextMenuStrip.Items[2].Visible = false;
                if (uniTreeView.SelectedNode.Tag is TObject && ((TObject)uniTreeView.SelectedNode.Tag).CLASS_ID != 0)
                {
                    itemContextMenuStrip.Items[1].Visible = itemContextMenuStrip.Items[2].Visible = true;                   
                }
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uniTreeView.SelectedNode.Tag is TObject)
            {
                InsertForm form = null;
                if (uniTreeView.SelectedNode.Tag is TObject && ((TObject)uniTreeView.SelectedNode.Tag).CLASS_ID != 0)
                {
                    form = new InsertForm((TObject)uniTreeView.SelectedNode.Tag);
                }
                else
                {
                    form = new InsertForm(new TObject());
                }
                if (form.ShowDialog() == DialogResult.OK)
                {
                    TreeNode pt = uniTreeView.SelectedNode;
                    if (uniTreeView.SelectedNode.Nodes.Count == 0)
                    {
                        pt.Nodes.Add(new TreeNode());
                    }
                    if (pt.IsExpanded)
                    {
                        pt.Collapse();
                        pt.Expand();
                    }
                }
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uniTreeView.SelectedNode.Tag is TObject)
            {
                TObject tobj = (TObject)uniTreeView.SelectedNode.Tag;

                TObjectEditForm form = ReflectionLogic.CreateFormByTObjectClassId(tobj.DataBaseName, tobj.CLASS_ID);

                form.SetForm(tobj,
                ClientAppViaWebService.Forms.TObjectEditForm.eMode.mdEdit,
                tobj.DataBaseName);

                if (form.ShowDialog() == DialogResult.OK)
                {
                    uniTreeView.SelectedNode.Text = form.ObjName;
                }
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uniTreeView.SelectedNode.Tag is TObject)
            {
                if (MessageBox.Show(this, "Are you sure want to delete this node?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    TObject tobj = (TObject)uniTreeView.SelectedNode.Tag;
                    //here must be cascade deleting by service                 

                    service.DeleteTobject(tobj);
                    uniTreeView.Nodes.Remove(uniTreeView.SelectedNode);
                }
            }
        }

        public void LoadNavigator(string rootId)
        {
            RootId = rootId;
            try
            {
                service = new ClientAppViaWebService.BaseLogicServiceRef.BaseLogicService();

                //uniTreeView.BeforeExpand += uniTreeView_BeforeExpand;
                if (RootId == "0")  TreeViewLogic.InitTreeView(service, ref uniTreeView, ref itemContextMenuStrip);
                else TreeViewLogic.InitTreeViewByRootExtId(service, ref uniTreeView, ref itemContextMenuStrip, RootId);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load the navigation tree. Can't connect to the remote web service. Go to the 'Setup->Run  WebServices Setup' menu tab to set the correct settings'");
            }
        }

        public void LoadNavigatorFilteredByClassIds(int [] classIds)
        {           
            try
            {
                service = new ClientAppViaWebService.BaseLogicServiceRef.BaseLogicService();
                uniTreeView.BeforeExpand -= uniTreeView_BeforeExpand;
                TreeViewLogic.InitTreeViewFilteredByClassIds(service, ref uniTreeView, ref itemContextMenuStrip, classIds);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load the navigation tree. Can't connect to the remote web service. Go to the 'Setup->Run  WebServices Setup' menu tab to set the correct settings'");
            }
        }
        public void LoadNavigatorForSelectedNode(string extId)
        {           
            try
            {
                service = new ClientAppViaWebService.BaseLogicServiceRef.BaseLogicService();

                TObject objToExpand = service.SelectTObject_By_Ext_Id(extId);

                TreeNode nodeToExpand = new TreeNode(objToExpand.NAME);
                nodeToExpand.Tag = objToExpand;
                nodeToExpand.ImageIndex = objToExpand.CLASS_ID;
                nodeToExpand.Nodes.Add(string.Empty);
                nodeToExpand.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);
                nodeToExpand.ContextMenuStrip = itemContextMenuStrip;
                uniTreeView.SelectedNode = nodeToExpand;

                TreeViewLogic.InitTreeForSelectedNode(service, ref uniTreeView, ref itemContextMenuStrip,objToExpand.EXT_ID,  nodeToExpand);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load the navigation tree. Can't connect to the remote web service. Go to the 'Setup->Run  WebServices Setup' menu tab to set the correct settings'");
            }
        }
              
    }
}
